package com.gosling.cloudspace.tools;

import cn.hutool.core.util.StrUtil;

/**
 * 根据业务需要新增的综合工具类方法
 *
 * @author yanghongquan
 * @email 842592135@qq.com
 * @date 2019/7/4 9:22
 */
public class CompTool {

    /**
     * 手机号明文+*(前3后4)
     *
     * @param pnumber
     * @return
     */
    public static String getPhoneNumberShield(String pnumber) {
        if (StrUtil.isBlank(pnumber)) {
            return "";
        }
        return pnumber.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    /**
     * 银行卡号明文+*(前6后4)
     *
     * @param bankCardNum
     * @return
     */
    public static String getBankCardNumberShield(String bankCardNum) {
        if (StrUtil.isBlank(bankCardNum)) {
            return "";
        }
        if(bankCardNum.length() < 10) {
            return bankCardNum;
        }
        if (bankCardNum.length() == 16) {
            return bankCardNum.replaceAll("(\\d{6})\\d{6}(\\d{4})", "$1******$2");
        }
        if (bankCardNum.length() == 18) {
            return bankCardNum.replaceAll("(\\d{6})\\d{8}(\\d{4})", "$1********$2");
        }
        return bankCardNum.replaceAll("(\\d{6})\\d{" + (bankCardNum.length() - 10) + "}(\\d{4})", "$1********$2");
    }

    /**
     * 身份证号明文+*(前1后1)
     *
     * @param idCardNum
     * @return
     */
    public static String getIdCardNumberShield(String idCardNum) {
        if (StrUtil.isBlank(idCardNum)) {
            return "";
        }
        return idCardNum.replaceAll("(\\d{1})\\d{16}(\\d{1})", "$1******$2");
    }

    /**
     * 个人姓名明文+*
     *
     * @param name
     * @return
     */
    public static String getNameShield(String name) {
        if (StrUtil.isBlank(name)) {
            return "";
        }
        int length = name.length();
        if (length == 1) {
            return name;
        }
        return name.replaceFirst(name.substring(0, 1), "*");
    }

}
